<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

include( './admin/model/admin/member.php' );
$m_member = new Member();

if( $action == 'index' )
{
	$page = get_post( 'page' );
	$page = $page?$page:1;
	$type = get_post( 'type' );
	$type = $type?$type:'all';
	$id = get_post( 'id' );
	$id = $id?$id:0;
	$member_no = $m_member->get_member_no( $type, $id );
	include( SUN_CLASS.'pagination.php' );
	$page_config = array
	(
			'base_url' => $script.'?m=member&a=index&type='.$type,
			'cur_page' => $page,
			'num_pages' => ceil( $member_no/20 )
	);
	//实例化分页类
	$pagination = new Pagination( $page_config );
	//生成分页
	$page_link = $pagination->create_links();
	$members = $m_member->get_member_list( ( $page-1 )*20, $type, $id );
	$group_tree = $m_member->get_group_tree();
	include $temp->display( 'admin/mb_list' );
	unset( $type, $page, $article_no, $page_link, $members );
}
elseif( $action == 'addmb' )
{
	$group_tree = $m_member->get_group_tree();
	include $temp->display( 'admin/mb_add_member' );
}
elseif( $action == 'addmember' )
{
	$basic['group_id'] = get_post( 'group_id' );
	$basic['username'] = get_post( 'username' );
	$basic['password'] = get_post( 'password' );
	$datas['email'] = get_post( 'email' );
	$datas['validate'] = get_post( 'validate' );
	$datas['sex'] = get_post( 'sex' );
	$datas['birthday'] = get_post( 'birthday' );
	$datas['qq'] = get_post( 'qq' );
	$datas['msn'] = get_post( 'msn' );
	$datas['integral'] = get_post( 'integral' );
	$datas['money'] = get_post( 'money' );
	$datas['contribute'] = get_post( 'contribute' );
	$datas['reputation'] = get_post( 'reputation' );
	$datas['homepage'] = get_post( 'homepage' );
	$datas['signature'] = get_post( 'signature' );
	$datas['face'] = get_post( 'face' );
	$datas['introduce'] = get_post( 'introduce' );
	$datas['register_time'] = date( 'Y-m-d H:i:s' );
	if( $m_member->add_member( $basic, $datas ) )
	{
		$success = '新用户添加成功！
					<a href="'.$script.'?m=member&a=addmb">添加新用户</a>
					<a href="'.$script.'?m=member">返回用户列表</a>';
		jump( $success, $script.'?m=member', 3 );
	}
	else
	{
		alert( '新用户添加失败，请联系管理员！' );
		go_back();
	}
	unset( $basic, $datas );
}
elseif( $action == 'editmb' )
{
	$id = get_post( 'id' );
	$group_tree = $m_member->get_group_tree();
	$member = $m_member->get_member( $id );
	include $temp->display( 'admin/mb_edit_member' );
}
elseif( $action == 'updatemb' )
{
	$id = get_post( 'id' );
	$basic['group_id'] = get_post( 'group_id' );
	$basic['username'] = get_post( 'username' );
	$basic['password'] = get_post( 'password' );
	$datas['email'] = get_post( 'email' );
	$datas['validate'] = get_post( 'validate' );
	$datas['sex'] = get_post( 'sex' );
	$datas['birthday'] = get_post( 'birthday' );
	$datas['qq'] = get_post( 'qq' );
	$datas['msn'] = get_post( 'msn' );
	$datas['integral'] = get_post( 'integral' );
	$datas['money'] = get_post( 'money' );
	$datas['contribute'] = get_post( 'contribute' );
	$datas['reputation'] = get_post( 'reputation' );
	$datas['homepage'] = get_post( 'homepage' );
	$datas['signature'] = get_post( 'signature' );
	$datas['face'] = get_post( 'face' );
	$datas['introduce'] = get_post( 'introduce' );
	$datas['register_time'] = date( 'Y-m-d H:i:s' );
	if( $m_member->update_member( $id, strim( $basic ), strim( $datas ) ) )
	{
		$success = '用户资料修改成功！
					<a href="'.$script.'?m=member&a=editmb&id='.$id.'">返回继续修改</a>
					<a href="'.$script.'?m=member&a=addmb">添加新用户</a>
					<a href="'.$script.'?m=member">返回用户列表</a>';
		jump( $success, $script.'?m=member', 3 );
	}
	else
	{
		alert( '用户资料修改失败，请联系管理员！' );
		go_back();
	}
	unset( $basic, $datas );
}
elseif( $action == 'delmb' )
{
	$ids = get_post( 'ids' );
	if( $m_member->delete( $ids ) )
	{
		jump( '系统提示：用户删除成功，页面跳转中。。。', $script.'?m=member&a=index', 1 );
	}
	else
	{
		alert( '用户删除失败，请联系管理员！' );
		go_back();
	}
}
elseif( $action == 'group' )
{
	$member_group = $m_member->get_group_tree();
	$group_1 = $member_group['用户组'];
	$group_2 = $member_group['系统组'];
	include $temp->display( 'admin/mb_group' );
}
elseif( $action == 'addgroup' )
{
	$datas = get_post( 'data' );
	$datas = json_decode( stripslashes( $datas ), true );
	$id = $m_member->addgroup( strim( $datas ) );
	echo $id?json_encode( array( 'ok' => 1, 'id' => $id ) ):json_encode( array( 'ok' => 0 ) );
}
elseif( $action == 'editgroup' )
{
	$type = get_post( 'type' );
	$id = get_post( 'id' );
	$datas = $m_member->get_power( $id );
	$power = $datas['power'];
	$name = $datas['name'];
	$grade = $datas['grade'];
	$upgrade_points = $datas['upgrade_points'];
	$edit_temp = $type == 'user'?'admin/mb_edit_group_user':'admin/mb_edit_group_system';
	include $temp->display( $edit_temp );
}
elseif( $action == 'updategp' )
{
	$id = get_post( 'id' );
	$type = get_post( 'type' );
	$power = get_post( 'power' );
	$name = get_post( 'name' );
	$grade = get_post( 'grade' );
	$upgrade_points = get_post( 'upgrade_points' );
	if( $m_member->update_group( $id, $power, $name, $grade, $upgrade_points ) )
	{
		$success = '用户组权限修改成功！
					<a href="'.$script.'?m=member&a=editgroup&type='.$type.'&id='.$id.'">返回继续修改</a>
					<a href="'.$script.'?m=member&a=group">返回用户组列表</a>';
		jump( $success, $script.'?m=member&a=group', 3 );
	}
	else
	{
		alert( '用户组权限修改失败，请联系管理员！' );
		go_back();
	}
	unset( $id, $type, $data );
}
elseif( $action == 'delgroup' )
{
	$id = get_post( 'id' );
	echo $m_member->delgroup( $id )?"{'ok':1}":"{'ok':0}";
}
elseif( $action == 'search' )
{
	$rule = get_post( 'rule' );
	$key = get_post( 'key' );
	$member = $m_member->search( strim( $rule ), strim( $key ) );
	if( empty( $member ) )
	{
		alert( '未搜素到相关用户！' );
		go_back();
		exit;
	}
	$group_tree = $m_member->get_group_tree();
	include $temp->display( 'admin/mb_edit_member' );
}
elseif( $action == 'validate' )
{
	$type = get_post( 'type' );
	$type = $type == 'validate'?1:0;
	$ids = get_post( 'ids' );
	echo $m_member->validate( $ids, $type, 'validate' )?"{'ok':1}":"{'ok':0}";
}
elseif( $action == 'banspeak' )
{
	$type = get_post( 'type' );
	$type = $type == 'banspeak'?1:0;
	$ids = get_post( 'ids' );
	echo $m_member->validate( $ids, $type, 'ban_speak' )?"{'ok':1}":"{'ok':0}";
}
elseif( $action == 'move' )
{
	$ids = get_post( 'ids' );
	$group_id = get_post( 'gid' );
	if( $m_member->move( $ids, $group_id ) )
	{
		jump( '系统提示：用户移动成功，页面跳转中。。。', $script.'?m=member&a=index', 1 );
	}
	else
	{
		alert( '用户移动失败，请联系管理员！' );
		go_back();
	}
}
elseif( $action == 'stat' )
{
	$groups = $m_member->get_group_stat();
	include $temp->display( 'admin/mb_stat' );
}
elseif( $action == 'uptgpb' )
{
	$group = get_post( 'group' );
	if( $m_member->uptgpb( strim( $group ) ) )
	{
		jump( '系统提示：用户组资料修改成功，页面跳转中。。。', $script.'?m=member&a=group', 1 );
	}
	else 
	{
		alert( '用户组资料修改失败，请联系管理员！' );
		go_back();
	}
}
unset( $m_member );